package cn.jhz.learn.blog.controller.core.auth;

import static java.util.stream.Collectors.toList;

import cn.jhz.learn.blog.common.bean.JsonData;
import cn.jhz.learn.blog.component.shiro.PermissionName;
import cn.jhz.learn.blog.po.file.BlogFile;
import cn.jhz.learn.blog.service.core.auth.file.ConsoleFileService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/auth/core/file/")
public class ConsoleFileController {

    private final ConsoleFileService fileService;

    @Autowired
    public ConsoleFileController(ConsoleFileService fileService) {
        this.fileService = fileService;
    }

    @PostMapping(value = "picture/uploads")
    @RequiresPermissions("file:post")
    @PermissionName("postPicture")
    public JsonData postPicture(@RequestParam("File") List<MultipartFile> files){

        return JsonData.success(fileService.batchUploadPicture(files).stream()
                .map(BlogFile::getSignatureCode)
                .collect(toList()));
    }
}
